package com.qiantu.phone.ui.activity;

import android.text.TextUtils;

import androidx.recyclerview.widget.RecyclerView;
import com.alibaba.fastjson.JSONObject;
import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.db.LLDeviceActionDBManager;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.db.LLFloorDBManager;
import com.qiantu.api.db.LLRoomDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.FloorBean;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.api.entity.SceneDetailBean;
import com.qiantu.api.entity.SceneInterruptModeBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.adapter.SceneDetailAdapter;
import com.qiantu.phone.ui.uiutil.DeviceType;
import com.qiantu.phone.utils.TempDataHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class EditSceneActivityNew extends EditSceneBaseActivity {
    private SceneDetailAdapter mSceneDetailAdapter;

    @Override
    protected void initData() {
        sceneDetailBean = TempDataHolder.getEditSceneDetailBean(1);
        if (sceneDetailBean == null) {
            SceneDetailBean addScene = new SceneDetailBean();
            addScene.setHouseSerialNo(AppApplication.getInstance().getCurHouseInfo().getHouseSerialNo());
            String roomSerialNo = getString("roomSerialNo");
            if(roomSerialNo != null){
                int type = getInt("type");
                if(type == 1){
                    RoomBean roomBean = LLRoomDBManager.getInstance(getContext()).getRoomByRoomSerialNo(roomSerialNo);
                    addScene.setRoomSerialNo(roomSerialNo);
                    addScene.setRoomName(roomBean.getRoomName());
                    addScene.setFloorSerialNo(roomBean.getFloorSerialNo());
                    addScene.setFloorName(roomBean.getFloorName());
                    addScene.setLinkType(1);
                }else if(type == 3){
                    FloorBean floorBean = LLFloorDBManager.getInstance(getContext()).getFloorByFloorSerialNo(roomSerialNo);
                    addScene.setRoomSerialNo("");
                    addScene.setRoomName("");
                    addScene.setFloorSerialNo(roomSerialNo);
                    addScene.setFloorName(floorBean.getFloorName());
                    addScene.setLinkType(3);
                }
            }else{
                addScene.setRoomSerialNo(RoomBean.ROOM_COMMON_SERIALNO);
            }
            addScene.setFavourite(true);
            addScene.setSceneType(1);
            addScene.setSceneTasks(new ArrayList<>());
            TempDataHolder.setEditSceneDetailBean(addScene);
            sceneDetailBean = addScene;
            String[] iconUrls = AppApplication.getInstance().getSceneIconsByThemeId();
            if (iconUrls.length > 0) {
                sceneDetailBean.setImage(iconUrls[0]);
            }
        }
        initFavourite = sceneDetailBean.getFavourite();
        initFavouriteRoomSerialNo = sceneDetailBean.getRoomSerialNo();
        if (sceneDetailBean.getSceneTasks() == null) {
            sceneDetailBean.setSceneTasks(new ArrayList<>());
        }else{
            List<SceneDetailBean.SceneTaskBean> newTasks = new ArrayList<>();
            int time = 0;
            for(int i = 0;i < sceneDetailBean.getSceneTasks().size();i++){
                SceneDetailBean.SceneTaskBean bean = sceneDetailBean.getSceneTasks().get(i);
                if(bean.getSceneTaskType() == 1){
                    SceneDetailBean.SceneDeviceInstructBean deviceInstructBean = bean.getSceneDeviceInstruct();
                    Map<String,String> map = deviceInstructBean.getDeviceItemStateData().getStatus();
                    if(map.get("red") != null) {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("red",map.get("red"));
                        jsonObject.put("green",map.get("green"));
                        jsonObject.put("blue",map.get("blue"));
                        map.remove("red");
                        map.remove("green");
                        map.remove("blue");
                        map.put("colorValue",jsonObject.toString());
                    }
                    if(map.get("songid") != null) {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("songname",map.get("songname"));
                        jsonObject.put("songpicurl",map.get("songpicurl"));
                        jsonObject.put("musicid",map.get("songid"));
                        map.remove("songname");
                        map.remove("songid");
                        map.remove("songpicurl");
                        map.put("songinfo",jsonObject.toString());
                    }
                    deviceInstructBean.setTriggerTime(time + deviceInstructBean.getTriggerTime());
                }else if(bean.getSceneTaskType() == 5){
                    bean.getSceneDeviceGroupInstruct().setTriggerTime(time + bean.getSceneDeviceGroupInstruct().getTriggerTime());
                }
                if(bean.getSceneTaskType() == 2){
                    time += bean.getSceneDelayedTask().getLongTime();
                }else{
                    newTasks.add(bean);
                }
            }
            sceneDetailBean.setSceneTasks(newTasks);
        }
        mSceneDetailAdapter = new SceneDetailAdapter(this, sceneDetailBean);
        mSceneDetailAdapter.targetSerialNo = getString("targetSerialNo");
        mRecyclerView.setAdapter(mSceneDetailAdapter);
        if (!TextUtils.isEmpty(sceneDetailBean.getSceneSerialNo())) {
            setTitle(getString(R.string.edit_scene));
            isCreate = false;
        } else {
            setTitle(getString(R.string.create_scene));
            isCreate = true;
        }
        isRecommendScene = getBoolean("isRecommendScene");
        if(isRecommendScene){
            setTitle(getString(R.string.edit_scene));
        }
    }

    @Override
    protected boolean submitCheck() {
        if (TextUtils.isEmpty(sceneDetailBean.getName())) {
            toast(R.string.please_set_name);
            return false;
        }
        if (TextUtils.isEmpty(sceneDetailBean.getImage())) {
            toast(R.string.please_select_icon);
            return false;
        }
        List<SceneDetailBean.SceneTaskBean> sceneTasks = sceneDetailBean.getSceneTasks();
        if (sceneTasks == null || sceneTasks.size() == 0) {
            toast(R.string.please_add_valid_task);
            return false;
        }
        boolean isAllDelayTask = true;
        for (int i = 0;i < sceneTasks.size();i++) {
            SceneDetailBean.SceneTaskBean task = sceneTasks.get(i);
            if (task.getSceneTaskType() != 2) {
                isAllDelayTask = false;
            }
            if(task.getSceneTaskType() == 1){
                SceneDetailBean.SceneDeviceInstructBean bean = task.getSceneDeviceInstruct();
                    SceneDetailBean.DeviceItemStateDataBean itemStateDataBean = bean.getDeviceItemStateData();
                    if(itemStateDataBean.getStatus().get("colorValue") != null){
                        Map<String, String> map = itemStateDataBean.getStatus();
                        JSONObject jsonObject =  JSONObject.parseObject(map.get("colorValue"));
                        itemStateDataBean.getStatus().put("red",jsonObject.getString("red"));
                        itemStateDataBean.getStatus().put("green",jsonObject.getString("green"));
                        itemStateDataBean.getStatus().put("blue",jsonObject.getString("blue"));
                        itemStateDataBean.getStatus().remove("colorValue");
                        sceneDetailBean.getSceneTasks().get(i).getSceneDeviceInstruct().setDeviceItemStateData(itemStateDataBean);
                    }
                    if(itemStateDataBean.getStatus().get("songinfo") != null){
                        Map<String, String> map = itemStateDataBean.getStatus();
                        JSONObject jsonObject =  JSONObject.parseObject(map.get("songinfo"));
                        itemStateDataBean.getStatus().put("songname",jsonObject.getString("songname"));
                        itemStateDataBean.getStatus().put("songpicurl",jsonObject.getString("songpicurl"));
                        itemStateDataBean.getStatus().put("songid",jsonObject.getString("musicid"));
                        itemStateDataBean.getStatus().remove("songinfo");
                        sceneDetailBean.getSceneTasks().get(i).getSceneDeviceInstruct().setDeviceItemStateData(itemStateDataBean);
                    }
            }
        }
        if (isAllDelayTask) {
            toast(R.string.please_add_valid_task);
            return false;
        }
        return true;
    }

    @Override
    protected RecyclerView.Adapter getAdapter() {
        return mSceneDetailAdapter;
    }

    @Override
    public void onResume(){
        super.onResume();
        if(!isCreate){
            LLHttpManager.getInterrupData(this, sceneDetailBean.getSceneSerialNo(), new HttpCallback<HttpData<SceneInterruptModeBean>>(this){
                @Override
                public void onSucceed(HttpData<SceneInterruptModeBean> result) {
                    super.onSucceed(result);
                    SceneInterruptModeBean sceneInterruptModeBean = result.getData();
                    sceneDetailBean.setHasActionSetting(sceneInterruptModeBean.getEnable());
                    mSceneDetailAdapter.notifyItemChanged(0, "updateInterrupt");
                }
            });
        }
    }
}